﻿using AppFramework.Admin.Services.Account;
using AppFramework.Admin.Services.Navigation;
using AppFramework.Admin.Services.Notification;
using AppFramework.App.Client.ApiClient;
using AppFramework.Shared.Consts;
using AppFramework.Shared.Extensions.Threading;
using AppFramework.Shared.Models;
using AppFramework.Shared.Services.Permission;
using AppFramework.Shared.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AppFramework.Admin.ViewModels
{
    public class MainTabsViewModel: NavigationViewModel
    {
        public MainTabsViewModel() { }



        #region 字段/属性

        public NavigationService NavigationService { get; set; }
        public NotificationService notificationService { get; set; }
        public IApplicationService appService { get; init; }
        public DelegateCommand<NavigationItem> NavigateCommand { get; private set; }

        private bool notificationPanelIsOpen;
        private bool isShowFriendsPanel;
        private readonly IRegionManager regionManager;
        private readonly IApplicationContext applicationContext;

        public bool NotificationPanelIsOpen
        {
            get { return notificationPanelIsOpen; }
            set
            {
                notificationPanelIsOpen = value;
                if (value)
                {
                    AsyncRunner.Run(notificationService.GetNotifications());
                }
                OnPropertyChanged();
            }
        }

        private bool isShowUserPanel;

        public bool IsShowUserPanel
        {
            get { return isShowUserPanel; }
            set
            {
                if (value && !isShowUserPanel)
                {
                    regionManager.Regions[AppRegions.UserPanel].RequestNavigate(AppViews.UserPanel);
                }
                isShowUserPanel = value;
                OnPropertyChanged();
            }
        }

        public bool IsShowFriendsPanel
        {
            get { return isShowFriendsPanel; }
            set
            {
                if (value && !isShowFriendsPanel)
                {
                    regionManager.Regions[AppRegions.UserPanel].RequestNavigate(AppViews.Friends);
                }

                isShowUserPanel = isShowFriendsPanel = value;

                OnPropertyChanged();
                OnPropertyChanged(nameof(IsShowUserPanel));
            }
        }

        public DelegateCommand SeeAllNotificationsCommand { get; private set; }
        public DelegateCommand SettingsCommand { get; private set; }
        public DelegateCommand SetAllNotificationsAsReadCommand { get; private set; }
        public DelegateCommand SetNotificationAsRead { get; private set; }

        #endregion



        public void Navigate(NavigationItem item)
        {
            if (item == null) return;

            NavigationService.Navigate(item.PageViewName);
        }

        public override async Task OnNavigatedToAsync(NavigationContext navigationContext)
        {
            IsShowUserPanel = false;

            await appService.GetApplicationInfo();

            if (applicationContext.Configuration.Auth.GrantedPermissions.ContainsKey(AppPermissions.HostDashboard))
                NavigationService.Navigate(AppViews.Dashboard);
        }


    }
}
